package com.sh.caryr.erp.controller.product;


import cn.afterturn.easypoi.util.PoiPublicUtil;
import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.entity.product.Attribute;
import com.sh.caryr.erp.excel.ImportFromExcel;
import com.sh.caryr.erp.exception.RestServiceException;
import com.sh.caryr.erp.service.IAttributeService;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 属性取值表 前端控制器
 * </p>
 *
 * @author milkeeper
 * @since 2017-11-22
 */
@RestController
@RequestMapping("/api/attribute")
public class AttributeController {
    @Autowired
    private IAttributeService attributeService;

    @ApiOperation(value = "获取attribute列表信息", notes = "获取attribute列表信息")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<Attribute>> findAttributes() {
        return new ResultMessage<>(attributeService.selectList(null));
    }

    @ApiOperation(value = "获取单个attribute信息", notes = "获取单个attribute信息")
    @ApiImplicitParam(name = "id", value = "attributeId", required = true, dataType = "long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<Attribute> findAttributeById(@PathVariable long id){
        return  new ResultMessage<>(attributeService.selectById(id));
    }

    @ApiOperation(value="创建attribute", notes="根据attribute对象创建attribute")
    @ApiImplicitParam(name = "attribute", value = "attribute详细实体", required = true, dataType = "Attribute")
    @RequestMapping(method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<Attribute> insertAttribute(@RequestBody Attribute attribute) {
        if(attributeService.insert(attribute)) {
            return new ResultMessage<Attribute>(attribute);
        }else {
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }
    }

    @ApiOperation(value="更新attribute", notes="根据attribute对象更新attribute")
    @ApiImplicitParam(name = "attribute", value = "attribute详细实体", required = true, dataType = "Attribute")
    @RequestMapping(method=RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<Attribute> updateAttributeById(@RequestBody Attribute attribute) {
        if(attributeService.updateById(attribute)) {
            return new ResultMessage<Attribute>(attribute);
        }else {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
    }

    @ApiOperation(value="批量删除attribute", notes="根据url的id来指定删除attribute")
    @ApiImplicitParam(name = "ids", value = "attributeIds", required = true, dataType = "String")
    @Transactional
    @RequestMapping(value = "/{ids}",method=RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage deleteAttributeById(@PathVariable String ids) {
        List<Long> list = UtilityTool.convertStringArray2LongList(ids.split(","));
        if(attributeService.deleteBatchIds(list)){
            return new ResultMessage();
        }else {
            throw new RestServiceException(ResultCode.DELETE_NOT_FOUND);
        }
    }

    @ApiOperation(value="批量导入attribute", notes="excel导入attribute")
    @Transactional
    @RequestMapping(value = "/excel",method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<Attribute>> importAttributFromExcel(){
        File file = new File(PoiPublicUtil.getWebRootPath("WEB-INF/classes/import/attribute.xlsx"));

        List<Attribute> attributeList = ImportFromExcel.importAttributes(file);

        attributeService.insertBatch(attributeList);

        return new ResultMessage<>(attributeList);
    }
	
}
